package activiti3;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.ArrayList;
import java.util.List;

public class ActivitiUtils {


    /**
     * 当前用户--》当前用户正在执行的任务--》piid-->流程实例
     */
    public static List<ProcessInstance> getPIByUser(String assignee){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<ProcessInstance> pis = new ArrayList<ProcessInstance>();
        /**
         * 该用户正在执行的任务
         */
        List<Task> tasks = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee(assignee)
                .list();
        if(null != tasks && tasks.size()>0){
            /**
             * 根据task-->piid-->activityId
             *                                            -->activitiImpl
             * 根据task-->pdid-->getProcessDefinitionId
             */
            for(Task task : tasks){
                String piid = task.getProcessInstanceId();
                ProcessInstance pi = processEngine.getRuntimeService()
                        .createProcessInstanceQuery()
                        .processInstanceId(piid)
                        .singleResult();
                pis.add(pi);
            }
        }
        return pis;
    }
    /**
     * 根据当前登陆人能够推导出所在的流程定义
     */
    public static void getProcessInstance(String assignee){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<ProcessInstance> pis = new ArrayList<ProcessInstance>();
        /**
         * 该用户正在执行的任务
         */
        List<Task> tasks = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee(assignee)
                .list();
        for(Task task : tasks){
            String pdid = task.getProcessDefinitionId();
            final ProcessDefinition processDefinition = processEngine.getRepositoryService()
                    .createProcessDefinitionQuery()
                    .processDefinitionId(pdid)
                    .singleResult();
        }
    }





}
